<?php
namespace app\modules\admin\controllers;

use app\models\Agent;
use app\models\UserCardRecharge;
use Yii;
use yii\web\NotFoundHttpException;

class AnalyseController extends BaseController{
	public function actionRechargeAnalyse(){
		$search_start_time = strtotime($this->get('search_start_time'));
		$search_end_time = strtotime($this->get('search_end_time'));
		$today = date('Y-m-d');
		if(!$search_end_time){
			$current = date('Y-m-d');
		}else{
			$current = $this->get('search_end_time');
		}
		if($search_start_time == $search_end_time){
			$start = $search_start_time - (7*86400);
		}else{
			$start = $search_start_time;
		}
		if($this->get('search_end_time') == date('Y-m-d')){
			$today = date('Y-m-d');
		}elseif($this->get('search_end_time') != date('Y-m-d')){
			$today = $this->get('search_end_time');
		}
		$days = ($search_end_time - $start)/86400;
		$last_month = date('Y-m-d',strtotime("$today - $days day"));
		$username = $this->get('search_agentname');
		$agent = Agent::find()->asArray()->where(['username'=>$username])->one();
		$trade_type = $this->get('search_trade_type');
		$recharge_type = $this->get('search_recharge_type');
		$source = $this->get('search_source');
		$month = array();
		$recharge = array();
		$renew = array();
		$reserve = array();
		$execute = array();
		for($i=0;$i<=$days;$i++){
			$months = date('m-d',strtotime("$last_month + $i day"));
			$mon = date('Y-m-d',strtotime("$last_month + $i day"));
			$end_time = strtotime($mon) + 86399;
			array_push($month,$months);
			$first_recharge = UserCardRecharge::find()->andFilterWhere(['between','create_time',strtotime($mon),$end_time])->andFilterWhere(['recharge_type'=>1])->andFilterWhere(['agent_id'=>$agent['id']])->andFilterWhere(['trade_type'=>$trade_type])->andFilterWhere(['recharge_type'=>$recharge_type])->andFilterWhere(['source'=>$source])->count();
			array_push($recharge,$first_recharge);
			$renews = UserCardRecharge::find()->andFilterWhere(['between','create_time',strtotime($mon),$end_time])->andFilterWhere(['recharge_type'=>2])->andFilterWhere(['agent_id'=>$agent['id']])->andFilterWhere(['trade_type'=>$trade_type])->andFilterWhere(['recharge_type'=>$recharge_type])->andFilterWhere(['source'=>$source])->count();
			array_push($renew,$renews);
			$reserves = UserCardRecharge::find()->andFilterWhere(['between','create_time',strtotime($mon),$end_time])->andFilterWhere(['recharge_type'=>3])->andFilterWhere(['agent_id'=>$agent['id']])->andFilterWhere(['trade_type'=>$trade_type])->andFilterWhere(['recharge_type'=>$recharge_type])->andFilterWhere(['source'=>$source])->count();
			array_push($reserve,$reserves);
			$executes = UserCardRecharge::find()->andFilterWhere(['between','create_time',strtotime($mon),$end_time])->andFilterWhere(['recharge_type'=>4])->andFilterWhere(['agent_id'=>$agent['id']])->andFilterWhere(['trade_type'=>$trade_type])->andFilterWhere(['recharge_type'=>$recharge_type])->andFilterWhere(['source'=>$source])->count();
			array_push($execute,$executes);
			
		}

		return $this->render('recharge_analyse',[
			'last_month' => $last_month,
			'today' => $current,
			'month' => json_encode($month),
			'recharge' => $recharge,
			'renew'   => $renew,
			'reserve' => $reserve,
			'execute' => $execute
		]);
	}
	//充值套餐图表统计
	public function actionRechargePackageAnalyse(){
		$search_start_time = strtotime($this->get('search_start_time'));
		$search_end_time = strtotime($this->get('search_end_time'));
		$end = date('Y-m-d');
		//判断是否选择开始时间，如果选择时间返回选择的时间，没有选择时间的话自动选择当前时间的前7天
		if(!$search_start_time){
			$start_time = strtotime($end) - (7*86400);
			$start = date('Y-m-d',strtotime(date('Y-m-d',$start_time)));
		}else{
			$start_time = $search_start_time;
			$start = date('Y-m-d',$start_time);
		}
		//判断是否选择结束时间，如果选择了时间返回选择的时间，没有选择时间的话自动选择当天
		if(!$search_end_time){
			$current = date('Y-m-d');
			$end_time = strtotime($current) + 86399;
		}else{
			$current = $this->get('search_end_time');
			$end_time = strtotime($current) + 86399;
		}
	
		$username = $this->get('search_agentname');
		$agent = Agent::find()->asArray()->where(['username'=>$username])->one();
		$trade_type = $this->get('search_trade_type');
		$recharge_type = $this->get('search_recharge_type');
		$source = $this->get('search_source');

		$menuList = (new \yii\db\Query())
        ->select(['user_card_recharge.id as id','package.id as pid','agent.id as agent_id','agent.username as username','package.name as pname','count(*) as pcount'])
        ->from('user_card_recharge')
        ->join('join','package','user_card_recharge.package_id=package.id')
        ->join('join','agent','user_card_recharge.agent_id=agent.id')
        ->groupBy('package.id')
        ->andFilterWhere(['user_card_recharge.agent_id'=>$agent['id']])
        ->andFilterWhere(['user_card_recharge.trade_type'=>$trade_type])
        ->andFilterWhere(['user_card_recharge.recharge_type'=>$recharge_type])
        ->andFilterWhere(['user_card_recharge.source'=>$source])
        ->andFilterWhere(['between','user_card_recharge.create_time',$start_time,$end_time])
        ->andFilterWhere(['user_card_recharge.is_show'=>1])
        ->all();
        
        $menu = array();
        $record = array();
        foreach ($menuList as $key => $value) {
        	$package = $menuList[$key]['pname'];
        	array_push($menu,$package);	
        	$countList = UserCardRecharge::find()->andFilterWhere(['package_id'=>$menuList[$key]['pid']])->andFilterWhere(['agent_id'=>$agent['id']])->andFilterWhere(['trade_type'=>$trade_type])->andFilterWhere(['recharge_type'=>$recharge_type])->andFilterWhere(['source'=>$source])->andFilterWhere(['between','user_card_recharge.create_time',$start_time,$end_time])->andFilterWhere(['user_card_recharge.is_show'=>1])->count();
        	array_push($record,$countList);
        }
		return $this->render('recharge_package_analyse',[
			'menu' => $menu,
			'record' => $record,
			'start' => $start,
			'end' => $current
		]);
	}
	//展示代理商流量卡使用平均用量
	public function actionAverageUsage(){
		$username = $this->get('search_agentname');
		if(empty($username)){
			$agent = "all";
		}else{
			$agent = $username;
		}
		$redis = Yii::$app->redis;
		$arr = $redis->hget('myhash',$agent);
		if(empty($arr)){
			throw new NotFoundHttpException('没有数据，请检查代理商名称是否填写正确！');
		}else{
			$count = array_slice(json_decode($arr),0,12);
			$average = array_slice(json_decode($arr),12,23);
		}
		$month = array();
		for ($i=1; $i <13 ; $i++) { 
			$months = date('Y-m',strtotime("-$i month"));
			array_push($month,$months);
		}
		
		return $this->render('average_usage',[
			'month' => $month,
			'count' => $count,
			'average' => $average
		]);
	}
	
	
}